<?php
/** 
 * Physician_model
 * This class handles model work necessary for Physician Crud and List operations 
 * 
 * @author Ray Nowell
 *	
 */ 
class Physician_model extends MY_Model {

	function Physician_model()
	{
		parent::MY_Model();
	}
	
	function Add_physician($data)
	{
		return $this->db->insert('physician', $data);
	}
	
	function Update_physician($data)
	{
		$this->db->where('physician_id',element('physician_id', $data));
		$this->db->where('facility_id',$this->facility_id);
		
		return $this->db->update('physician',$data);
	}
	
	function Delete_physician()
	{
		$ret_val = $this->delete_record('physician','physician_id');
		return $ret_val;
	}
	
	function get_physician_group_lookup($current_physician_id)
	{
		$this->db->select('physician_group_id, `group`');
		$where 	 =	'facility_id	=	'.$this->facility_id.' AND (';
		$where	.=	'active = 1 ';
		$where	.=	$current_physician_id	?	'OR physician_group_id = '.$current_physician_id	: 	'';
		$where	.=	')';
		$this->db->where($where);
		$this->db->order_by('`group`');
		$query = $this->db->get('physician_group');
				
		return $query->result_array(); 
	}
	
	/*
	 * Returns a list of current physicians
	 * @todo must filter by logged in user
	 */
	function get_physician_list()
	{
		$select_fields = "physician_id
										 ,last_name
										 ,first_name
										 ,`group`
										 ,CASE physician.active WHEN 1 THEN 'Yes' Else 'No' END as active";
		//Get the query results for the clinical physicians
		$this->db->select($select_fields,false);
		$this->db->from('physician');
		$this->db->join('physician_group', 'physician.physician_group_id = physician_group.physician_group_id', 'left');
		$this->db->where('physician.facility_id',$this->facility_id);
		$this->db->order_by('last_name, first_name');
		$query = $this->db->get();
				
		return $query->result_array();
	}


	/*
	 * Returns a single physician record based on Cliincal Physician Id passed 
	 */
	function get_physician_record($physician_id)
	{
		 $query = $this->db->limit(1)->get_where('physician', array('physician_id' => $physician_id,
		 																'facility_id' => $this->facility_id));
		 
		 return $query->row();
	}	
}

/* End of file physician_model.php */
/* Location: ./system/application/models/physician_model.php */